﻿package ${packageEnums};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;
import ${project}${importIService}.${"I"+model.pojo+"Service"};
import ${project}.auto.dao.AutoDao;
import ${project}.auto.entity.${model.pojo};
import ${project}.auto.entity.${model.pojo+"Example"};

import java.util.List;
<#--导入-->
<#if model.listFullyQualifiedName?? && (model.listFullyQualifiedName?size > 0) >
<#list model.listFullyQualifiedName as mykey>
import ${mykey};
</#list>
</#if>
/**
* @Title ${tableRemarks}
* @Package ${packageEnums}
* @Description service层实现类
* @author ${author}
* @date ${datetime}
*/
@Service
@Transactional//事务管理
public class ${className} implements ${"I"+model.pojo+"Service"} {

	@Autowired
	protected AutoDao mapper;

	/**
	 * 根据主键查询是否存在记录
	 * 
	 * @param id 主键
	 * @return
	 */
	@Override
	public boolean isExistsRecord(${primaryKeyType} id){
		${model.pojo+"Example"} example = new ${model.pojo+"Example"}();
		${model.pojo+"Example"}.Criteria cra = example.createCriteria();
		cra.andIdEqualTo(id);
		int recordSize = mapper.${'${model.pojo+"Dao"}'?uncap_first}.countByExample(example);
		if (recordSize == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 根据条件查询是否存在记录
	 * 
	 * @param example
	 * @return
	 */
	@Override
	public boolean isExistsRecord(${model.pojo+"Example"} example){
		int recordSize = mapper.${'${model.pojo+"Dao"}'?uncap_first}.countByExample(example);
		if (recordSize == 0) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 根据主键查询符合条件的个数
	 * 
	 * @param id 主键
	 * @return
	 */
	@Override
	public int countByPrimaryKey(${primaryKeyType} id){
		${model.pojo+"Example"} example = new ${model.pojo+"Example"}();
		example.createCriteria().andIdEqualTo(id);
		int recordSize = mapper.${'${model.pojo+"Dao"}'?uncap_first}.countByExample(example);
		if (recordSize == 0) {
			return 0;
		} else {
			return recordSize;
		}
	}
	
	/**
	 * 根据条件查询符合条件的个数
	 * 
	 * @param example
	 * @return
	 */
	@Override
	public int countByExample(${model.pojo+"Example"} example){
		int recordSize = mapper.${'${model.pojo+"Dao"}'?uncap_first}.countByExample(example);
		return recordSize;
	}

	/**
	 * 根据主键获取唯一的一条记录
	 * 
	 * @param id 主键
	 * @return
	 */
	@Override
	public ${model.pojo} getByPrimaryKey(${primaryKeyType} id){
		return mapper.${'${model.pojo+"Dao"}'?uncap_first}.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据条件获取唯一的一条记录,如果没有查到,则返回空,如果查到多条,
     * 只返回第一条,判断是否只有一小记录,请使用{countByExample}方法
	 * @param example
	 * @return
	 */
	@Override
	public ${model.pojo} getByExample(${model.pojo+"Example"} example){
		List${"<"+model.pojo+">"} ${model.pojo}List = mapper.${'${model.pojo+"Dao"}'?uncap_first}.selectByExample(example);
		if (${model.pojo}List != null && ${model.pojo}List.size() > 0) {
			return ${model.pojo}List.get(0);
		}
		return null;
	}
	
	/**
	 * 根据条件查询符合条件的列表
	 * 
	 * @param example
	 * @return
	 */
	@Override
	public List${"<"+model.pojo+">"} searchListByExample(${model.pojo+"Example"} example){
		return mapper.${'${model.pojo+"Dao"}'?uncap_first}.selectByExample(example);
	}
	
	/**
	 * 插入,返回记录的主键Id
	 * 
	 * @param record
	 * @return 记录的主键Id
	 */
	@Override
	public ${primaryKeyType} insert(${model.pojo} record){
		mapper.${'${model.pojo+"Dao"}'?uncap_first}.insert(record);
		return record.getId();
	}

	/**
	 * 批量插入,返回成功个数
	 * 
	 * @param recordList
	 * @return
	 */
	@Override
	public int batchInsert(List${"<"+model.pojo+">"} recordList){
		int result = 0;
		for (int i = 0; i < recordList.size(); i++) {
			${model.pojo} record = recordList.get(i);
			mapper.${'${model.pojo+"Dao"}'?uncap_first}.insert(record);
			result++;
		}
		return result;
	}

	// =======================================
	// ===================删===================
	// =======================================
	/**
	 * 根据条件物理删除(不可恢复)符合条件的,返回成功个数
	 * 
	 * @param example
	 * @return
	 */
	@Override
	public int deleteByExample(${model.pojo+"Example"} example){
		mapper.${'${model.pojo+"Dao"}'?uncap_first}.deleteByExample(example);
		return 1;
	}

	/**
	 * 根据主键物理删除(不可恢复),返回成功个数
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(${primaryKeyType} id){
		mapper.${'${model.pojo+"Dao"}'?uncap_first}.deleteByPrimaryKey(id);
		return 1;
	}

	/**
	 * 根据主键列表物理删除(不可恢复),返回成功个数
	 * 
	 * @param ids 
	 * @return
	 */
	@Override
	public int deleteByPrimaryKeys(List<${primaryKeyType}> ids){
		${model.pojo+"Example"} example = new ${model.pojo+"Example"}();
		${model.pojo+"Example"}.Criteria cra = example.createCriteria();
		cra.andIdIn(ids);
		return mapper.${'${model.pojo+"Dao"}'?uncap_first}.deleteByExample(example);
	}

	// =======================================
	// ===================改===================
	// =======================================

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 */
	@Override
	public int update(${model.pojo} record){
	//	if(record!=null&&<#if primaryKeyType == 'String'>StringUtils.isNotEmpty(record.getId())</#if><#if primaryKeyType == 'Long'>record.getId()!=null</#if>){
	//		${model.pojo} tempRecord=mapper.${'${model.pojo+"Dao"}'?uncap_first}.selectByPrimaryKey(record.getId() , ${model.pojo+"Example"}.createSearchColumn("id"));
	//		if(tempRecord!=null){
				return mapper.${'${model.pojo+"Dao"}'?uncap_first}.updateByPrimaryKey(record);
	//		}
	//	}
	}

	/**
	 * 更新记录(只更新更改了的行)
	 * 
	 * @param record
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(${model.pojo} record){
	//	if(record!=null&&<#if primaryKeyType == 'String'>StringUtils.isNotEmpty(record.getId())</#if><#if primaryKeyType == 'Long'>record.getId()!=null</#if>){
	//		${model.pojo} tempRecord=mapper.${'${model.pojo+"Dao"}'?uncap_first}.selectByPrimaryKey(record.getId() , ${model.pojo+"Example"}.createSearchColumn("id"));
	//		if(tempRecord!=null){
				return mapper.${'${model.pojo+"Dao"}'?uncap_first}.updateByPrimaryKeySelective(record);
	//		}
	//	}
	}
	
	/**
	 * 批量更新记录
	 * 
	 * @param recordList
	 * @return
	 */
	@Override
	public int batchUpdate(List${"<"+model.pojo+">"} recordList){
		int result = 0;
		for(int i=0;i ${"<"} recordList.size();i++){
			${model.pojo} record = recordList.get(i);
	//		if(record!=null&&<#if primaryKeyType == 'String'>StringUtils.isNotEmpty(record.getId())</#if><#if primaryKeyType == 'Long'>record.getId()!=null</#if>){
	//			${model.pojo} tempRecord=mapper.${'${model.pojo+"Dao"}'?uncap_first}.selectByPrimaryKey(record.getId() , ${model.pojo+"Example"}.createSearchColumn("id"));
	//			if(tempRecord!=null){
					result += mapper.${'${model.pojo+"Dao"}'?uncap_first}.updateByPrimaryKey(record);
	//			}
	//		}
		}
		return result;
	}
	
	/**
	 * 
	 * @Title saveChangeList
	 * @Description 更新更改过的记录
	 * @param insertedList
	 * @param deletedList
	 * @param updatedList
	 * @return
	 * @throws Exception
	 */
	@Override
	public int saveChangeList(List${"<"+model.pojo+">"} insertedList, List${"<"+model.pojo+">"} deletedList, List${"<"+model.pojo+">"} updatedList) {
		int result = 0;
		if (insertedList!=null&&insertedList.size()!=0) {
			for (int i = 0; i < insertedList.size(); i++) {
				result += mapper.${'${model.pojo+"Dao"}'?uncap_first}.insert(insertedList.get(i));
			}
		}
		if (deletedList!=null&&deletedList.size()!=0) {
			for (int i = 0; i < deletedList.size(); i++) {
				if(<#if primaryKeyType == 'String'>StringUtils.isNotEmpty(deletedList.get(i).getId())</#if><#if primaryKeyType == 'Long'>deletedList.get(i).getId()!=null</#if>){
					result += mapper.${'${model.pojo+"Dao"}'?uncap_first}.deleteByPrimaryKey(deletedList.get(i).getId());
				}
			}
		}
		if (updatedList!=null&&updatedList.size()!=0) {
			for (int i = 0; i < updatedList.size(); i++) {
				${model.pojo} record = updatedList.get(i);
	//			if(record!=null&&<#if primaryKeyType == 'String'>StringUtils.isNotEmpty(record.getId())</#if><#if primaryKeyType == 'Long'>record.getId()!=null</#if>){
	//				${model.pojo} tempRecord = mapper.${'${model.pojo+"Dao"}'?uncap_first}.selectByPrimaryKey(record.getId(), ${model.pojo+"Example"}.createSearchColumn("id"));
	//				if (tempRecord != null) {
						result += mapper.${'${model.pojo+"Dao"}'?uncap_first}.updateByPrimaryKey(record);
	//				}
	//			}
			}
		}
		return result;
	}
	/*
	*==========================================
	*==================其它更新=================
	*==========================================
	*/
<#list model.tablesFields as tablesField>
	/**
	* 根据主键更新${tablesField.fieldsName} [${tablesField.fieldCommon}]
	* @param id 主键
	* @param ${tablesField.fieldsName} [${tablesField.fieldCommon}]
	* @return
	*/
	@Override
	public int update${'${tablesField.fieldsName}'?cap_first}(${primaryKeyType} id, ${tablesField.fieldType} ${tablesField.fieldsName}){
		${model.pojo} record = new ${model.pojo}();
		record.setId(id);
		record.set${'${tablesField.fieldsName}'?cap_first}(${tablesField.fieldsName});
		return mapper.${'${model.pojo+"Dao"}'?uncap_first}.updateByPrimaryKeySelective(record);
	}

	/**
	* 根据主键批量更新${tablesField.fieldsName} [${tablesField.fieldCommon}]
	* @param idList 主键列表
	* @param ${tablesField.fieldsName} [${tablesField.fieldCommon}]
	* @return
	*/
	@Override
	public int batchUpdate${'${tablesField.fieldsName}'?cap_first}(List<${primaryKeyType}> idList, ${tablesField.fieldType} ${tablesField.fieldsName}){
		${model.pojo+"Example"} example = new ${model.pojo+"Example"}();
		example.createCriteria().andIdIn(idList);
		${model.pojo} record = new ${model.pojo}();
		record.set${'${tablesField.fieldsName}'?cap_first}(${tablesField.fieldsName});
		return mapper.${'${model.pojo+"Dao"}'?uncap_first}.updateByExampleSelective(record, example);
	}
</#list>
}